/*
 * Copyright (c) 田梓萱 2021-2022.
 * All Rights Reserved.
 * All codes are protected by China's regulations on the protection of computer software, and infringement must be investigated.
 * 版权所有 (c) 田梓萱 2021-2022.
 * 所有代码均受中国《计算机软件保护条例》保护，本项目已获得软著.
 */

package com.xcl.calculator;

import ohos.aafwk.ability.Ability;
import ohos.app.Context;
import ohos.data.DatabaseHelper;
import ohos.data.preferences.Preferences;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.UUID;

/**
 * The type Preference rw.
 *
 * @author Xcl
 * @date 2022 /1/12
 * @package com.xcl.calculator
 */
public class Preference_RW extends Ability {
    /**
     * The constant counterKey2.
     */
    private static final String counterKey2 = "key2";
    /**
     * The constant counterKey3.
     */
    private static final String counterKey3 = "key3";
    /**
     * The constant label.
     */
    private static final HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, 0x00234, "Preference_RW");
    /**
     * The constant preferenceFile.
     */
    private static final String preferenceFile = "messages_tzx_xcl";
    /**
     * The constant counterKey.
     */
    private static final String counterKey = "key0";
    /**
     * The constant counterKey1.
     */
    private static final String counterKey1 = "key1";
    /**
     * The constant counterKey7.
     */
    private static final String counterKey7 = "DeBug";
    /**
     * The constant counterKey8.
     */
    private static final String counterKey8 = "PushBug";
    /**
     * The constant context.
     */
    public static Context context;
    /**
     * The constant NetStatus.
     */
    public static int NetStatus = 0;
    /**
     * The constant uuid.
     */
    public static UUID uuid;

    /**
     * 写入
     *
     * @param key     the key
     * @param message the message
     */
    private static void writeCounter(String key, int message) {
        try {
            DatabaseHelper databaseHelper = new DatabaseHelper(context);
            Preferences preferences = databaseHelper.getPreferences(preferenceFile);
            preferences.putInt(key, message);
            preferences.flush();
        } catch (Exception e) {
            HiLog.warn(label, "xcl写发生错误[" + e.getMessage() + "],key=[" + key + "],message=[" + message + "]");
        }
    }

    /**
     * 写入
     *
     * @param key     the key
     * @param message the message
     */
    private static void writeCounter(String key, long message) {
        try {
            DatabaseHelper databaseHelper = new DatabaseHelper(context);
            Preferences preferences = databaseHelper.getPreferences(preferenceFile);
            preferences.putLong(key, message);
            preferences.flush();
        } catch (Exception e) {
            HiLog.warn(label, "xcl写发生错误[" + e.getMessage() + "],key=[" + key + "],message=[" + message + "]");
        }
    }

    /**
     * 读取
     *
     * @param key the key
     * @return int int
     */
    private static int readCounter(String key) {
        try {
            DatabaseHelper databaseHelper = new DatabaseHelper(context);
            Preferences preferences = databaseHelper.getPreferences(preferenceFile);
            return preferences.getInt(key, 0);
        } catch (Exception e) {
            HiLog.warn(label, "xcl读发生错误" + e.getMessage());
            return 0;
        }
    }

    /**
     * 读取
     *
     * @param key the key
     * @param a   the a
     * @return long
     */
    private static long readCounter(String key, int a) {
        try {
            XLog.info(label, "没必要的数据:" + a);
            DatabaseHelper databaseHelper = new DatabaseHelper(context);
            Preferences preferences = databaseHelper.getPreferences(preferenceFile);
            return preferences.getLong(key, 0);
        } catch (Exception e) {
            HiLog.warn(label, "xcl读发生错误" + e.getMessage());
            return 0;
        }
    }

    /**
     * Gets local device type.
     *
     * @return the local device type
     */
    public static int getLocalDeviceType() {
        return readCounter(counterKey1);
    }

    /**
     * Sets local device type.
     *
     * @param localDevice the local device
     */
    public static void setLocalDeviceType(int localDevice) {
        Preference_RW.writeCounter(counterKey1, localDevice);
    }

    /**
     * Ff 1 r int.
     *
     * @return the int
     */
    public static int ff1_r() {
        return readCounter(counterKey);
    }

    /**
     * Ff 1 w.
     */
    public static void ff1_w() {
        Preference_RW.writeCounter(counterKey, 1);
    }

    /**
     * Ff 2 w.
     */
    public static void ff2_w() {
        Preference_RW.writeCounter(counterKey, 2);
    }

    /**
     * Ff 3 r int.
     *
     * @return the int
     */
    public static long ff3_r() {
        return readCounter(counterKey2, 0);
    }

    /**
     * Ff 3 w.
     *
     * @param a3 the a 3
     */
    public static void ff3_w(long a3) {
        Preference_RW.writeCounter(counterKey2, a3);
    }

    /**
     * Ff 4 r int.
     *
     * @return the int
     */
    public static long ff4_r() {
        return readCounter(counterKey3, 0);
    }

    /**
     * Ff 4 w.
     *
     * @param a3 the a 3
     */
    public static void ff4_w(long a3) {
        Preference_RW.writeCounter(counterKey3, a3);
    }

    /**
     * Ff 7 r int.
     *
     * @return the int
     */
    public static int ff7_r() {
        return readCounter(counterKey7);
    }

    /**
     * Ff 7 w.
     *
     * @param a1 the a 1
     */
    public static void ff7_w(int a1) {
        Preference_RW.writeCounter(counterKey7, a1);
    }

    /**
     * Ff 8 r int.
     *
     * @return the int
     */
    public static int ff8_r() {
        return readCounter(counterKey8);
    }

    /**
     * Ff 8 w.
     *
     * @param a1 the a 1
     */
    public static void ff8_w(int a1) {
        Preference_RW.writeCounter(counterKey8, a1);
    }
}
